package com.example.shop.pic;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Administrator on 2016/12/29.
 */

public class CompressPic {
    private String path;
    private String pathY;
    private Bitmap mBitmapOrigin;
    private void compress(){
        try {
            mBitmapOrigin= BitmapFactory.decodeFile(path);
            ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
            int quality = 100;

            while (true){
                byteArrayOutputStream.reset();
                mBitmapOrigin.compress(Bitmap.CompressFormat.JPEG, quality, byteArrayOutputStream);
                int size = byteArrayOutputStream.size();
                System.out.println("xxxxxxxxxxxxxxx");
                if (size>100*1024) {
                    quality-=2;
                }else {
                    break;
                }
            }
            System.out.println("quality = ---------------" + quality);
            FileOutputStream fileOutputStream = new FileOutputStream(pathY);
            mBitmapOrigin.compress(Bitmap.CompressFormat.JPEG, quality, fileOutputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
